<?php
class Auth extends Database
{
    public function login($username,$password,$table_username,$table_password,$table)
    {
        $user_password = functions::encrypt_decrypt("encrypt",$password);
        //$db = new Database();
        $login = $this->query("SELECT *
                                FROM {$table}
                                WHERE {$table_username}={$this->escape($username)} AND
                                {$table_password}={$this->escape($user_password)} AND is_active = 1","row");
        if($login){
            $this->setAuth('current_user',$login);
            setcookie('USERAUTH',serialize($login),0,'/',COOKIE_URL);
            return $login;
        }
        return false;
    }
    
    public function hasAuth($name)
    {
        if(!isset($_SESSION))
        {
            @session_start();
        }
        if(isset($_SESSION[$name]))
        {
            return true;
        }
        return false;
    }
    
    public function setAuth($name,$value)
    {
        if(!isset($_SESSION))
        {
            @session_start();
        }
        $_SESSION[$name] = $value;
        return $this;
    }
    
    public function getAuth($name)
    {
        if(!isset($_SESSION))
        {
            @session_start();
        }
        if(isset($_SESSION[$name]))
        {
            return $_SESSION[$name];
        }
        return false;
    }
    
    public function destroyAuth($name)
    {
        if(!isset($_SESSION))
        {
            @session_start();
        }
        if(isset($_SESSION[$name]))
        {
            unset($_SESSION[$name]);
            return true;
        }
        return false;
    }
    

}



